Single Image Processing

In [1]:
#from tools import init
import os
import tools
import glob
import cv2
import numpy as np
import pickle
import matplotlib.gridspec as gridspec
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
import scipy
from scipy import signal
from collections import deque

out_dir = 'output_images/step2/'

tools.init()

Undistort test images with visualization

In [2]:
# Undistort test images with visualization
%matplotlib inline

images = glob.glob('test_images/*.jpg')

gs = gridspec.GridSpec(8, 2)
gs.update(wspace=0.01, hspace=0.02) # set the spacing between axes.
plt.figure(figsize=(8,2))

for idx, fname in enumerate(images):
    img = cv2.imread(fname)
    dst = tools.undistort_img(img)
    
    f, (ax1, ax2) = plt.subplots(1, 2, figsize=(20,10))
    ax1.imshow(cv2.cvtColor(img, cv2.COLOR_BGR2RGB))
    ax1.set_title('Original Image', fontsize=30)
    ax2.imshow(cv2.cvtColor(dst, cv2.COLOR_BGR2RGB))
    ax2.set_title('Undistorted Image', fontsize=30)
    
    image_name=os.path.split(fname)[1]
    write_name = out_dir + 'undistorted_' + image_name
    cv2.imwrite(write_name,dst)
    
    #cv2.imshow('undistorted', dst)
    #cv2.waitKey(500)

cv2.destroyAllWindows()
<matplotlib.figure.Figure at 0x2893b030a20>

Image binarization

In [3]:
def gaussian_blur(img, kernel_size=3):
    return cv2.GaussianBlur(img, (kernel_size, kernel_size), 0)

def binarize(img,
             s_thresh=(90, 255),
             l_thresh=(40, 255),
             sx_thresh=(20, 100), ksize_sx=3#11
            ):    
    # Convert to HLS color space and separate the L & S channels
    hls = cv2.cvtColor(img, cv2.COLOR_RGB2HLS).astype(np.float)
    l_channel = hls[:,:,1]
    s_channel = hls[:,:,2]
    
    # Sobel x
    sobelx = cv2.Sobel(l_channel, cv2.CV_64F, 1, 0, ksize=ksize_sx) # Take the derivative in x
    abs_sobelx = np.absolute(sobelx) # Absolute x derivative to accentuate lines away from horizontal
    scaled_sobel = np.uint8(255*abs_sobelx/np.max(abs_sobelx))
    
    # Threshold x gradient
    sxbinary = np.zeros_like(scaled_sobel)
    sxbinary[(scaled_sobel >= sx_thresh[0]) & (scaled_sobel <= sx_thresh[1])] = 1
    
    # Threshold color channel
    s_binary = np.zeros_like(s_channel)
    s_binary[(s_channel >= s_thresh[0]) & (s_channel <= s_thresh[1])] = 1
    
    # Threshold lightness
    l_binary = np.zeros_like(l_channel)
    l_binary[(l_channel >= l_thresh[0]) & (l_channel <= l_thresh[1])] = 1
    
    binary = np.zeros_like(l_binary)
    binary[(l_binary == 1) & (s_binary == 1) | (sxbinary == 1)] = 1
    
    kernel = np.ones((3, 3), binary.dtype)
    # remove white blobs
    #binary = cv2.morphologyEx(binary, cv2.MORPH_OPEN, kernel)
    # fill black holes
    #binary = cv2.morphologyEx(binary, cv2.MORPH_CLOSE, kernel)
    
    # Stack each channel
    # Note color_binary[:, :, 0] is all 0s, effectively an all black image. It might
    # be beneficial to replace this channel with something else.
#    color_binary = np.dstack(( np.zeros_like(sxbinary), sxbinary, s_binary))
    color_binary = np.dstack((l_binary, sxbinary, s_binary))
    binary = (np.dstack(( binary, binary, binary))*255.).astype('uint8')
    
    return binary, color_binary

def binarize_img(img):
    binary,_ = binarize(img)
    return binary
In [4]:
image = mpimg.imread('test_images/test5.jpg')
image = tools.undistort_img(image)
_,color_binary = binarize(image)

assert(color_binary is not None)
plt.imsave(out_dir + 'binary_test5.jpg', color_binary)

# Plot the result
f, (ax1, ax2) = plt.subplots(1, 2, figsize=(24, 9))
f.tight_layout()

ax1.imshow(image)
ax1.set_title('Original Image', fontsize=40)

ax2.imshow(color_binary)
ax2.set_title('Binarized Image', fontsize=40)
plt.subplots_adjust(left=0., right=1, top=0.9, bottom=0.)
In [5]:
images = glob.glob('test_images/*.jpg')

gs = gridspec.GridSpec(8, 2)
gs.update(wspace=0.01, hspace=0.02) # set the spacing between axes.
plt.figure(figsize=(8,2))

for idx, fname in enumerate(images):
    image = mpimg.imread(fname)
    image = tools.undistort_img(image)
#    image = gaussian_blur(image)
    binary = binarize_img(image)
    
    f, (ax1, ax2) = plt.subplots(1, 2, figsize=(20,10))
    ax1.imshow(image)
    ax1.set_title('Original Image', fontsize=30)
    ax2.imshow(binary)
    ax2.set_title('Binarized Image', fontsize=30)
    
    image_name=os.path.split(fname)[1]
    write_name = out_dir + 'binary_' + image_name
    plt.imsave(write_name, binary)
    #print(write_name)

cv2.destroyAllWindows()
<matplotlib.figure.Figure at 0x28941a98080>

Perspective Transform

In [6]:
bird_corners = tools.birdview_corners()

image = mpimg.imread('test_images/straight_lines1.jpg')
image = tools.undistort_img(image)

corner_tuples = []
for i,_ in enumerate(bird_corners):
    corner_tuples.append(tuple(bird_corners[i]))

# draw: bottom-left, top-left, top-right, bottom-right
for i, j in [(0,1), (1,2), (2,3), (3,0)]:
    cv2.line(image, corner_tuples[i], corner_tuples[j], color=[255,0,0], thickness=1)

warped = tools.warp_img(image)

plt.imsave(out_dir + 'straight_lines.jpg', image)
plt.imsave(out_dir + 'warped_straight_lines.jpg', warped)

# Plot the result
f, (ax1, ax2) = plt.subplots(1, 2, figsize=(25, 10))
f.tight_layout()

ax1.set_title('Undistorted Image with source points drawn', fontsize=35)
ax1.tick_params(axis='both', which='major', labelsize=20)
ax1.imshow(image)

ax2.set_title('Warped result with dest. points drawn', fontsize=35)
ax2.tick_params(axis='both', which='major', labelsize=20)
ax2.imshow(warped)

plt.subplots_adjust(left=0., right=1, top=0.9, bottom=0.)

ROI to mask noise at the left and right sides of frame

In [7]:
image = mpimg.imread('test_images/test5.jpg')

image = tools.undistort_img(image)
warp = tools.warp_img(image)
warp_roi = tools.ROI(warp)

binary = binarize_img(image)
binary = tools.warp_img(binary)
binary_roi = tools.ROI(binary)

# Plot the result
f, (ax1, ax2) = plt.subplots(1, 2, figsize=(25, 10))
f.tight_layout()

ax1.set_title('Undistorted Image', fontsize=35)
ax1.tick_params(axis='both', which='major', labelsize=20)
ax1.imshow(image)

ax2.set_title('Warped result with ROI', fontsize=35)
ax2.tick_params(axis='both', which='major', labelsize=20)
ax2.imshow(warp_roi)

f, (ax1, ax2) = plt.subplots(1, 2, figsize=(25, 10))
f.tight_layout()

ax1.set_title('Warped Binary result', fontsize=35)
ax1.tick_params(axis='both', which='major', labelsize=20)
ax1.imshow(binary)

ax2.set_title('Warped binary result with RIO', fontsize=35)
ax2.tick_params(axis='both', which='major', labelsize=20)
ax2.imshow(binary_roi)
Out[7]:
<matplotlib.image.AxesImage at 0x2893fe5c3c8>

Finding Lanes

In [8]:
import numpy as np
import cv2
import matplotlib.pyplot as plt
from tools import binarize_pipeline
from detect_lane import find_peaks 
In [9]:
# Test find_peaks
from detect_lane import find_peaks 

img = mpimg.imread('test_images/test5.jpg')

binary = binarize_pipeline(img)
left_peak = find_peaks(binary, 300, verbose=True)    
right_peak = find_peaks(binary, 1000, verbose=True)    
pick value:  46145
peak before sigma:  321
peak_x after sigma:  327
C:\Development\Udacity\carnd\P4-Advanced-Lane-Finding\detect_lane.py:34: VisibleDeprecationWarning: using a non-integer number instead of an integer will result in an error in the future
  histogram = np.sum(binary[binary.shape[0]/2:,:], axis=0)
*** final peak_x:  327
pick value:  10971
peak before sigma:  1009
peak_x after sigma:  1018
*** final peak_x:  1018
In [10]:
# Detect left and right lines
from detect_lane import detect_line, draw_lanes_with_windows 

%matplotlib inline

print('left_peak:', left_peak)
print('right_peak:', right_peak)

# Create an output image to draw on and  visualize the result
out_img = np.dstack((binary, binary, binary))*255

#left_peak = 0
#right_peak = 0

leftx = np.empty(shape=(0,0))
lefty = np.empty(shape=(0,0))
left_fit = np.empty(shape=(0,0))
left_win_rects = np.empty(shape=(0,0))
rightx = np.empty(shape=(0,0))
righty = np.empty(shape=(0,0))
right_fit = np.empty(shape=(0,0))
right_win_rects = np.empty(shape=(0,0))

if left_peak > 0:
    (leftx, lefty), left_fit, left_win_rects = detect_line(binary, left_peak, verbose=True)
if right_peak > 0:
    (rightx, righty), right_fit, right_win_rects = detect_line(binary, right_peak, verbose=True)

print('left_fit: ', left_fit)
print('right_fit: ', right_fit)
    
out_img = draw_lanes_with_windows(binary,
                                leftx, lefty, left_fit,
                                rightx, righty, right_fit,
                                left_win_rects, right_win_rects)

plt.figure(figsize=(8,8))
plt.xlim(0, 1280)
plt.ylim(720, 0) 
plt.imshow(out_img)
left_peak: 327
right_peak: 1018
left_fit:  [  2.69116708e-04  -4.35724969e-01   4.85375892e+02]
right_fit:  [  4.48009877e-04  -5.80763704e-01   1.19645845e+03]
Out[10]:
<matplotlib.image.AxesImage at 0x2894410ab70>
In [11]:
from detect_lane import detect_line_in_roi, draw_detect_line_in_roi

leftx1=[]
lefty1=[]
left_fit1=[]

if len(left_fit):
    (leftx1, lefty1), left_fit1 = detect_line_in_roi(binary, left_fit)
(rightx1, righty1), right_fit1 = detect_line_in_roi(binary, right_fit)

out_img = draw_detect_line_in_roi(binary, left_fit1, leftx1, lefty1, right_fit1, rightx1, righty1)

plt.figure(figsize=(8,8))
plt.imshow(out_img)
plt.xlim(0, out_img.shape[1])
plt.ylim(out_img.shape[0], 0)
Out[11]:
(720, 0)
In [15]:
from detect_lane import Line
from detect_lane import process_image_ex

def process_image(img):
    return process_image_ex(img, leftL, rightL, frame_Ind, verbose=Verbose)
In [16]:
Verbose=True
frame_Ind = 0;

# Read in a thresholded image
img = mpimg.imread('test_images/test5.jpg')

leftL  = Line(327, img.shape[1], 7)
rightL = Line(1018, img.shape[1], 7)
In [17]:
import numpy as np
import cv2
import matplotlib.pyplot as plt

result = process_image(img)
result = process_image(img)

plt.figure()
plt.figure(figsize=(10,8))
plt.imshow(result)
plt.show()
plt.imsave(out_dir + 'projected_lane_test5.jpg',result)
C:\Development\Udacity\carnd\P4-Advanced-Lane-Finding\detect_lane.py:34: VisibleDeprecationWarning: using a non-integer number instead of an integer will result in an error in the future
  histogram = np.sum(binary[binary.shape[0]/2:,:], axis=0)
pick value:  46145
peak before sigma:  321
peak_x after sigma:  327
*** final peak_x:  327
pick value:  10971
peak before sigma:  1009
peak_x after sigma:  1018
*** final peak_x:  1018
<matplotlib.figure.Figure at 0x2893b576828>
In [ ]: